package fragment.tabhostFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.PersonEntity;

import activity.RecodingActivity;
import application.BasisApplication;
import de.greenrobot.event.EventBus;
import fragment.AbsBaseFragment;
import fragment.LoginFragment;
import fragment.OpinionFragment;
import fragment.SetFragment;
import utils.roundimage.RoundedImageView;
import utils.texttools.StringUtil;

/**
 * 我
 * Created by dllo on 15/11/17.
 */

public class MePageFragment extends AbsBaseFragment implements View.OnClickListener {
    private RoundedImageView me_roundImageView, fragment_me_img;
    private TextView me_sound_recording, meSigTv, fragment_me_name;
    private RelativeLayout playHistory,clear,setting,advice
            ;
    private ImageView meUserBg;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this); //注册
    }


    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_me;
    }

    @Override
    protected void initView() {
        me_sound_recording = findView(R.id.me_sound_recording);
        fragment_me_img = findView(R.id.fragment_me_img);
        playHistory = findView(R.id.me_relative_playHistory);
        clear = findView(R.id.me_relative_binding);
        advice = findView(R.id.advice);
        meSigTv = findView(R.id.meSigTv);
        fragment_me_name = findView(R.id.fragment_me_name);
        meUserBg = findView(R.id.meUserBg);
        setting = findView(R.id.me_relative_setting);

    }

    @Override
    protected void initData() {
        setting.setOnClickListener(this);
        me_sound_recording.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentRecorin = new Intent(getContext(), RecodingActivity.class);
                startActivity(intentRecorin);
            }
        });
        fragment_me_img.setOnClickListener(this);//对头像的监听
        playHistory.setOnClickListener(this);
        clear.setOnClickListener(this);
        advice.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {//设置
            case R.id.me_relative_setting:
                goToMenu(new SetFragment());
                break;
            case R.id.fragment_me_img://个人中心
                if (BasisApplication.isLogin()){
                    goToMenu(new PersonalMesage());
                }else {
                goToMenu(new LoginFragment());
                }

                break;
            case R.id.me_relative_playHistory://收藏

                goToNext(new MyCollectionFragment());
                break;
            case R.id.me_relative_binding:
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        getContext());
                builder.setTitle("提示");
                builder.setMessage("清除缓存");
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setCancelable(false);
                builder.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getContext(), "正在为你清除", Toast.LENGTH_SHORT).show();
//                               //清除本地缓存
//                                DataCleanManager.cleanSharedPreference(getContext());
                            Toast.makeText(getContext(), "清除完成", Toast.LENGTH_SHORT).show();
                            }
                        });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                builder.create().show();

                break;
            case R.id.advice:
                goToMenu(new OpinionFragment());
                break;


        }
    }



//  PersonEntity entity

    public void onEventMainThread(PersonEntity entity) {

          if (entity!=null){
            if (StringUtil.isEmpty(entity.getName())){
                fragment_me_name.setText(entity.getPhoneNumber());
            }
            else {
                fragment_me_name.setText(entity.getName());
            }

              if (entity.getSignatye() != null) {
                meSigTv.setText(entity.getSignatye());
            } else {
                if (StringUtil.isEmpty(entity.getSignatye())){
                    meSigTv.setText("写点什么吧");
                }else {
                    meSigTv.setText(entity.getSignatye());
                }

            }
            if (entity.getUserImage() != null) {
                fragment_me_img.setImageBitmap(BytesToBimap(entity.getUserImage()));
            } else {
                fragment_me_img.setImageResource(R.mipmap.album_dialog_cat_cry);
            }
            if (entity.getUserBg() != null) {
                meUserBg.setImageBitmap(BytesToBimap(entity.getUserBg()));
            } else {
                meUserBg.setImageResource(R.mipmap.user_space_top_bg);
            }
        }
    }

    public void onEventMainThread(Boolean isLogin) {
        if (isLogin) {
            meSigTv.setVisibility(View.VISIBLE);
            if (StringUtil.isEmpty(BasisApplication.getUser().getName())){
                fragment_me_name.setText(BasisApplication.getUser().getPhoneNumber());
            }
            else {
                fragment_me_name.setText(BasisApplication.getUser().getName());
            }

            if (BasisApplication.getUser().getSignatye() != null) {
                meSigTv.setText(BasisApplication.getUser().getSignatye());
            } else {
                if (StringUtil.isEmpty(BasisApplication.getUser().getSignatye())){
                    meSigTv.setText("写点什么吧");
                }else {
                    meSigTv.setText(BasisApplication.getUser().getSignatye());
                }

            }
            if (BasisApplication.getUser().getUserImage() != null) {
                fragment_me_img.setImageBitmap(BytesToBimap(BasisApplication.getUser().getUserImage()));
            } else {
                fragment_me_img.setImageResource(R.mipmap.album_dialog_cat_cry);
            }
            if (BasisApplication.getUser().getUserBg() != null) {
                meUserBg.setImageBitmap(BytesToBimap(BasisApplication.getUser().getUserBg()));
            } else {
                meUserBg.setImageResource(R.mipmap.user_space_top_bg);
            }
        }else {
            meSigTv.setVisibility(View.GONE);
            fragment_me_name.setText("登录");
            fragment_me_img.setImageResource(R.mipmap.image_default_01);
            meUserBg.setImageResource(R.mipmap.user_space_top_bg);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);  //取消注册
    }

}
